#pragma once
#include "cocos2d.h"
#include "AppDelegate.h"
#include "EventHelper.h"
#include "MusicListManager.h"

USING_NS_CC;

class MusicList;
class MusicListItem;
class MusicLevelList;
class MusicLevelListItem;


class MusicList : public LayerColor, public EventHelper
{
	public:
	MusicList();
	static MusicList* create(Vec2 listpos, Size listarea, float itemheight, MusicListManager* plistmgr, MusicLevelListManager* plevelmgr);

	float itemheight;
	float itempadding = frameSize.height*0.01;
	Vec2 listPosition;
	Size listArea;
	TTFConfig itemTitleStyle;
	TTFConfig itemArtistStyle;
	FiniteTimeAction* actselect;
	FiniteTimeAction* acthover;
	int hoverindex = -1;
	int selectedindex = -1;
	MusicListItem* selectedItem;
	int openedLevelListIndex = -1;
	float openedLevelListHeight = 0;
	int selectedLevelIndex = 0;
	bool selectedSetChanged = false;
	int orbitAngelZ = 0;

	MusicListManager* listmgr;
	MusicLevelListManager* levelmgr;
	vector<MusicLevelListItem*> levelItems;

	private:
	bool isDragging = false;
	bool isLevelListAnime = false;
	Vec2 mousePosOnKeyDown;
	float listPosYOnKeyDown;

	public:
	void createList();
	void addItem(string setid, string title, string artist, string coverpath);
	virtual MusicListItem * getItemByIndex(int index);
	void openLevelList();
	void changeSelectedLevel();

	bool isHover(Event * e);
	void onMouseMove(Event * e);
	void onMouseDown(Event * e);
	void onMouseUp(Event * e);
};

class MusicListItem :public ClippingNode
{
	public:
	static MusicListItem* create(string setid, string title, string artist, string coverpath, MusicList* list);

	public:
	MusicList* plist;
	string setid;
	string title;
	string artist;
	string coverpath;

	bool onhover = false;
	bool onselect = false;

	Sprite* cover = nullptr;
	LayerGradient* covermask = nullptr;
	LayerColor* border = nullptr;
	LayerGradient* borderlayer = nullptr;
	Label* selectedlabel = nullptr;
	Label* labeltitle = nullptr;
	Label* labelartist = nullptr;

	FiniteTimeAction* actselect;
	FiniteTimeAction* acthover;

	public:
	void hover();
	void leave();
	void selected();
	void disselect();
};

class MusicLevelListItem : public LayerGradient, public EventHelper
{
	public:
	MusicLevelListItem() : EventHelper(this) {};
	static MusicLevelListItem* create(string id, string keys, string level, string difficult, Size size);

	string id;
	string keys;
	string level;
	string diff;

	void select();
	void disselect();
	bool isMouseHover();

	private:
	vector<LayerColor*> ptrs;
	bool mouseHover = false;
	Vec2 mouseDownPos;

	void onMouseMove(Event* e);
	void onMouseDown(Event* e);
	void onMouseUp(Event* e);
};


